from django.shortcuts import render
from drf_haystack.viewsets import HaystackViewSet
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView

from meiduo_mall.utils.pagination import StandardResultPagination
from.serializers import SKUSerializer, SKUIndexSerializer
from .models import SKU

# GET  /categories/(?P<category_id>\d+)/skus/
class SKUListView(ListAPIView):

    #指定排序后端，目的是为了ordering_fields
    filter_backends = [OrderingFilter]

    ordering_fields = ('create_time', 'price', 'sales')

    pagination_class = StandardResultPagination

    def get_queryset(self):

        category_id = self.kwargs.get('category_id')

        sku= SKU.objects.filter(category_id=category_id, is_launched=True)

        return sku

    # 指定序列化器
    serializer_class = SKUSerializer


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """

    index_modle = [SKU]

    serializer_class = SKUIndexSerializer